/*
 * tetrixboard.h - the place where all the action happens
 * Copyright (C) 2006-2007  Mithin Kumar L
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this application; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef GAME_WIDGET_H
#define GAME_WIDGET_H

#include <QFrame>
#include <QBasicTimer>
#include <QColor>
#include <QMap>

#include "tetrixpiece.h"

class QPainter;

class TetrixBoard : public QFrame
{
	Q_OBJECT

public:

	TetrixBoard(QWidget *parent = 0);

	QSize sizeHint() const;
    QSize minimumSizeHint() const;

public slots:

	void start();
	void setGamePaused(bool p);

signals:

	void ringsRemoved(int);

protected:
    void paintEvent(QPaintEvent *event);
    void keyPressEvent(QKeyEvent *event);
    void timerEvent(QTimerEvent *event);

private:

	void clearBoard();
	int timeoutTime();

	TetrixShape &shapeAt(int x, int y);
	void newPiece();

	int squareWidth();
    int squareHeight();

	bool overlaps();

	bool movePiece(int xDir,int yDir);
	void placePiece();

	void drawFront(int x,int y,bool r,QPainter *painter);
	void drawTop(int x,int y,QPainter *painter);
	void drawSide(int x,int y,QPainter *painter);

	void drawBoard(QPainter *painter);

	void findPosition(double &x,double &y,bool r);

	void initializeColorMap();

	enum { BoardWidth = 15, BoardHeight = 15 };

	QBasicTimer timer;
	
	bool isStarted;
    bool isPaused;
    bool isWaitingAfterLine;
    
	QMap<TetrixShape,QColor> colorMap;

	TetrixPiece curPiece;
    TetrixPiece nextPiece;
	
	double theta;

    int curX;
    int curY;
    
	//int numLinesRemoved;
    //int numPiecesDropped;
    //int score;
    //int level;
    
	TetrixShape board[BoardWidth][BoardHeight];

};

#endif